﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Chi_Tiet_San_Pham : System.Web.UI.Page
{
    DigitalWorldDataContext db = new DigitalWorldDataContext();
    string connectionString = WebConfigurationManager.ConnectionStrings["DAL.Properties.Settings.DigitalWorldConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        lbSelected.Text = "0 item(s)";
        if (Request.QueryString["ProID"] == null)
        {
            Response.Redirect("~/Trang-Chu.aspx");
        }
        else
        {
            SetMultiView1();
            LoadData();
            LoadCountItem();
        }
    }

    void LoadCountItem()
    {
        DataTable dtCart = Session["Cart"] as DataTable;
        if (dtCart.Rows.Count > 0)
        {
            lbSelected.Text = dtCart.Compute("SUM(Quantity)", "").ToString() + " item(s)";
        }
    }

    private void SetMultiView1()
    {
        var danhsach = db.products.SingleOrDefault
            (pr => pr.productid.Equals(int.Parse(Request.QueryString["ProID"])));
        string cateid = danhsach.categoryid;
        if (cateid.Equals("LT") || cateid.Equals("TL"))
            MultiView1.SetActiveView(View1);
        else
            MultiView1.SetActiveView(View2);
    }

    void LoadData()
    {
        int ProID = int.Parse(Request.QueryString["ProID"]);

        SqlConnection cnn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("select *from Products where ProductID = @ProID", cnn);
        cmd.Parameters.AddWithValue("@ProID", ProID);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        cnn.Close();

        fv_Details_Information.DataSource = dt;
        fv_Details_Information.DataBind();
        fv_Details_Information2.DataSource = dt;
        fv_Details_Information2.DataBind();
    }


    protected void btnDatHang_Click(object sender, EventArgs e)
    {
        int ProductID = int.Parse(Request.QueryString["ProID"]);
        DataTable dtCart = Session["Cart"] as DataTable;
        DataRow dtRow = dtCart.Rows.Find(ProductID);
        if (dtRow != null)
        {
            dtRow["Quantity"] = (int)dtRow["Quantity"] + 1;
        }
        else
        {
            var danhsach = db.products.SingleOrDefault
                (pr => pr.productid.Equals(ProductID));
            DataRow r = dtCart.NewRow();
            r["ProductID"] = danhsach.productid;
            r["Productname"] = danhsach.productname;
            r["CategoryID"] = danhsach.categoryid;
            r["SupplierID"] = danhsach.supplierid;
            r["Photopath"] = danhsach.photopath;
            r["UnitPrice"] = danhsach.unitprice;
            dtCart.Rows.Add(r);
        }
        LoadCountItem();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Tim-kiem.aspx?tk=" + txtsearch.Text);
    }
}